package com.gdit.zax.servletjsp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.FromRequest;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.Repost;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.WebUpload;
import com.gdit.zax.utils.webUtil;
import com.mysql.jdbc.StringUtils;

public class EditInfo extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FromRequest fr = new FromRequest(getServletContext(), request, response);
		String token = webUtil.getCookie(request, "LoginCookie");
		Connection conn = DBUtil.getConnection();
		
		String nick = fr.getParameterString("nick");
		String truename = fr.getParameterString("truename");
		int changepic = TextUtil.String2int(fr.getParameterString("changepic"),0); 
		int age = TextUtil.String2int(fr.getParameterString("age"),-1); 
		int sex = TextUtil.String2int(fr.getParameterString("sex"),0);
		String email = fr.getParameterString("email");
		String phone = fr.getParameterString("phone");
		String introduction = fr.getParameterString("introduction");
		String address = fr.getParameterString("address");
		FileItem headfile = (FileItem) fr.getParameter("pic");
		
		if (conn != null) {
			if (age > 0 && age <= 130) {
				UserDao udao = DAOFactory.init(conn, request, response).getUserDao();
				UserInfo info = udao.getInfo(Constant.LOGINTYPE_WEB, token);
				if (info != null) {
					info.setNick(nick);
					info.setTruename(truename);
					info.setAge(age);
					info.setSex(sex);
					info.setEmail(email);
					info.setPhone(phone);
					info.setIntroduction(introduction);
					info.setAddress(address);
					
					String headpicpath;
					if (changepic == 1 && headfile != null && !"".equals(headfile.getName())) {//选择了图片
						headpicpath = info.getId() + "." + WebUpload.getExtensionName(headfile.getName());
						if (WebUpload.save(getServletContext(), headfile, "userheads", headpicpath)){
							info.setUserhead("userheads/" + headpicpath);
						}
					}
					
					
					if (udao.updataInfo(info)){
						Repost.back(request, response);
//						Repost.message("修改成功!", request, response);
					} else {
						Repost.message("修改失败!", request, response);
					}
				} else {
					Repost.nologin(response);
				}
			} else {
				Repost.message("请输入正确的年龄(1-130)!", request, response);
			}
		} else {
			Repost.dberror(request, response);
		}
	}

}
